package com.tocean.demo.service;

import com.tocean.demo.entity.questionbank.User;
import com.tocean.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    /**
     * 增加用户信息
     */
    public Boolean addUser(User user){
        userRepository.save(user);
        return true;
    }

    /**
     * 查询用户信息
     */
    public User selectUser(Long id){
        User user = userRepository.findById(id).get();
        return user;
    }

    /**
     * 更改用户信息
     */
    public User updateUser(User user){
        Long id = user.getId();
        User originUser = this.selectUser(id);
        if(originUser.getRealname()!=user.getRealname())
            originUser.setRealname(user.getRealname());

        if(originUser.getNickname()!=user.getNickname())
            originUser.setNickname(user.getNickname());

        if(originUser.getPassword()!=user.getPassword())
            originUser.setPassword(user.getPassword());

        if(originUser.getGrade()!=user.getGrade())
            originUser.setGrade(user.getGrade());

        if(originUser.getPhoneNumber()!=user.getPhoneNumber())
            originUser.setPhoneNumber(user.getPhoneNumber());

        if(originUser.getEmail()!=user.getEmail())
            originUser.setEmail(user.getEmail());

        if(originUser.getLevelType()!=user.getLevelType())
            originUser.setLevelType(user.getLevelType());

        if(originUser.getLevelType()!=user.getLevelType())
            originUser.setLevelType(user.getLevelType());
        userRepository.deleteById(id);
        userRepository.save(originUser);
        return originUser;
    }


    public String deleteUser(String id){
        if(userRepository.findById(Long.valueOf(id))==null)
            return "not exist";
        else{
            userRepository.deleteById(Long.valueOf(id));
            return "Success";
        }

    }
}
